package me.hyman.testcustomview.viewDragHelper.drawer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import me.hyman.testcustomview.R;

/**
 * Created by luluteam on 2016/9/26.
 */
public class LeftMenuAdapter extends ArrayAdapter<MenuItem> {

    private LayoutInflater layoutInflater;
    private int selectedPos = -1;

    public LeftMenuAdapter(Context context, MenuItem[] objects) {
        super(context, -1, objects);
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_left_menu, parent, false);
        }

        TextView txtTitle = (TextView) convertView.findViewById(R.id.txt_menu_title);
        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.img_menu_icon);

        txtTitle.setText(getItem(position).text);
        imgIcon.setBackgroundResource(getItem(position).icon);

        if(position == selectedPos) {
            imgIcon.setBackgroundResource(getItem(position).iconSelected);
            convertView.setBackgroundColor(getContext().getResources().getColor(R.color.state_menu_item_selected));
        }

        return convertView;
    }

    public void setSelection(int position) {
        selectedPos = position;
        notifyDataSetChanged();
    }
}
